/*
 * ExplorerPanel.java
 *
 * Created on March 5, 2006, 9:14 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.events.SettingsChangedEvent;
import com.bff.slimserver.musicjukebox.lib.events.SettingsChangedListener;
import com.bff.bjj.ui.component.BJJPanel;
import org.bff.slimserver.SlimDatabase;
import org.bff.slimserver.SlimPlaylist;

/**
 *
 * @author  Bill
 */
public class ExplorerPanel extends BJJPanel implements SettingsChangedListener {

    private SlimDatabase database;
    private SlimPlaylist playlist;
    private ExplorerIface explorer;

    public ExplorerPanel(boolean useTreeExplorer) {
        super();
        try {
            this.database = JukeboxProperties.getInstance().getDatabase();
            this.playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();

        setExplorer(useTreeExplorer);

        explorer = explorerListPanel;

        JukeboxProperties.getInstance().addSettingsChangedListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        explorerListPanel = new com.bff.slimserver.musicjukebox.ui.ExplorerListPanel();

        setLayout(new java.awt.BorderLayout());
        add(explorerListPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.bff.slimserver.musicjukebox.ui.ExplorerListPanel explorerListPanel;
    // End of variables declaration//GEN-END:variables
    private ExplorerTreePanel explorerTreePanel;

    public void refreshComponents() {
        explorer.refreshComponents();
    }

    public SlimDatabase getDatabase() {
        return database;
    }

    public void setDatabase(SlimDatabase database) {
        this.database = database;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    public void setExplorer(boolean useTree) {
        if (useTree) {
            if (explorerListPanel != null) {
                remove(explorerListPanel);
            }
            explorerListPanel = null;
            explorerTreePanel = new ExplorerTreePanel(getDatabase(), getPlaylist());
            explorer = explorerTreePanel;
            add(explorerTreePanel, java.awt.BorderLayout.CENTER);
        } else {
            if (explorerTreePanel != null) {
                remove(explorerTreePanel);
            }
            explorerTreePanel = null;
            explorerListPanel = new ExplorerListPanel(getDatabase(), getPlaylist());
            explorer = explorerListPanel;
            add(explorerListPanel, java.awt.BorderLayout.CENTER);
        }
    }

    @Override
    public void settingsChanged(SettingsChangedEvent event) {
        setExplorer(JukeboxProperties.getInstance().isUseTreeExplorer());
    }
}
